﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace yule.CodeBuilder.Controllers
{

    /// <summary>
    /// 代码模板控制器
    /// </summary>
    public class CoreTemplateController : BaseController
    {
        /// <summary>
        /// 代码模板首页
        /// </summary>
        public ActionResult Index()
        {
            /*
            TimeSpan t1 = new TimeSpan(System.DateTime.Now.Ticks);
            for (int i = 0; i < 100 * 100; i++)
            {
                yule.Enum.Sys.UserState us = yule.Util.Convert.ToEnum<yule.Enum.Sys.UserState>(1);
            }
            TimeSpan t2 = new TimeSpan(System.DateTime.Now.Ticks);

            double a = (t2 - t1).TotalMilliseconds;
            */

            return this.View();

        }

        /// <summary>
        /// Request中若存在DownLoad;返回FileContentResult;进行下载
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="BuildType"></param>
        /// <returns></returns>
        public ActionResult CoreAction(string TableName, string BuildType)
        {
            Core.PDM.TableInfo model = Core.PDM.PdmReader.Default.Tables[TableName];
            if (this.Request["DownLoad"] != null)
            {
                //解析<代码模板>视图,生成代码.
                string core = yule.CodeBuilder.Core.ViewHelper.ParseView(this.ControllerContext);
                string fileDownFileName = "代码.cs";
                return yule.CodeBuilder.Core.DownLoadHelper.CreateDownLoad(core, fileDownFileName, yule.CodeBuilder.Core.DownLoadHelper.CONTENTTYPE_TEXT);
            }
            else
            {
                return this.PartialView(BuildType, model);
            }
        }

        public ActionResult Create()
        {
            string str = null;
            try
            {
                string[] tables = this.Request["tables"].Split(',');
                foreach (string tableName in tables)
                {
                    CreateCode(tableName);
                }
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }

            return this.Json(str);
        }

        private void CreateCode(string tableName)
        {
            string createPath = Server.MapPath("~/CreateCore/");

            Core.PDM.TableInfo table = Core.PDM.PdmReader.Default.Tables[tableName];

            CreateCode(createPath, Core.BuildType.BaseModel, table);
            CreateCode(createPath, Core.BuildType.Model, table);
            CreateCode(createPath, Core.BuildType.IDAL, table);
            CreateCode(createPath, Core.BuildType.BaseDAL, table);
            CreateCode(createPath, Core.BuildType.DAL, table);

        }

        private void CreateCode(string createPath, yule.CodeBuilder.Core.BuildType type, yule.CodeBuilder.Core.PDM.TableInfo table)
        {
            string core = yule.CodeBuilder.Core.ViewHelper.ParseView(this.ControllerContext, table, type.ToString());
            yule.CodeBuilder.Core.IBuilder builder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(table, type);
            string path = createPath + builder.Path;
            string fileName = createPath + builder.FileFullName;


            if (System.IO.Directory.Exists(path) == false)
            {
                System.IO.Directory.CreateDirectory(path);
            }
            if (System.IO.File.Exists(fileName) == true)
            {
                System.IO.File.Delete(fileName);
            }

            System.IO.StreamWriter stream = System.IO.File.CreateText(fileName);
            try
            {
                stream.Write(core);
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

    }

}